package com.tangzhe.demo06.controller;

import com.tangzhe.demo01.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 测试springMvc各种请求以及返回页面、对象、json串
 */
@Controller
@RequestMapping("demo06")
public class UserController {

    /**
     * 测试请求转发
     */
    @RequestMapping("/tologin")
    public String toLogin() {
        return "demo06";
    }

    /**
     * 测试post请求，返回user对象的json串
     */
    @RequestMapping(value="/login", method= RequestMethod.POST)
    @ResponseBody
    public User login(User user) {
        System.out.println(user);
        return user;
    }

    /**
     * 测试下面路径，返回user对象的json串
     */
    @RequestMapping("/test01/{username}/{password}")
    @ResponseBody
    public User test01(@PathVariable String username, @PathVariable String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        return user;
    }

    /**
     * 测试接收页面传过来的json串转为user对象
     */
    @RequestMapping("/test02")
    @ResponseBody
    public User test02(@RequestBody User user) {
        System.out.println(user);
        return user;
    }

    /**
     * 测试重定向
     */
    @RequestMapping("/test03")
    public String test03() {
        return "redirect:/demo06/tologin";
    }

}
